/*
 *  Copyright (C) 2010 Fabien Renaud
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.boc.botv.service.impl;

import com.boc.botv.common.Database;
import com.boc.botv.dao.SiteAccountDao;
import com.boc.botv.model.SiteAccount;
import com.boc.botv.service.SiteAccountManager;
import java.util.Date;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Fabien Renaud
 */
@Service("siteAccountManager")
//@Transactional(readOnly = true)
public class SiteAccountManagerImpl implements SiteAccountManager {

  private final Log logger = LogFactory.getLog(SiteAccountManagerImpl.class);
  private SiteAccountDao siteAccountDao;

  @Autowired
  public void setSiteAccountDao(SiteAccountDao siteAccountDao) {
    this.siteAccountDao = siteAccountDao;
  }

  public List<SiteAccount> getAccounts() {
         logger.info("[SiteAccountManager] Getting all SiteAccount");
    List<SiteAccount> sal = siteAccountDao.getAccounts();
    
    return sal;
  }

  public SiteAccount getAccount(Integer id) {
         logger.info("[SiteAccountManager] Getting SiteAccount #" + id);
    SiteAccount sa = siteAccountDao.getAccount(id);
    
    return sa;
  }

  //@Transactional(readOnly = false)
  public void createAccount(SiteAccount account) {
         logger.info("[SiteAccountManager] Creating SiteAccount with account");
    siteAccountDao.saveAccount(account);
    
  }

  //@Transactional(readOnly = false)
  public void changeName(int accountId, String name) {
         logger.info("[SiteAccountManager] Changing siteAccount name to `" + name + "`");
    SiteAccount account = siteAccountDao.getAccount(accountId);
    account.setName(name);
    
  }

  //@Transactional(readOnly = false)
  public void mergeAccounts(SiteAccount account1, SiteAccount account2, String name) {
         logger.info(String.format("[SiteAccountManager] Merging accounts #%1$d and %2$d under name `%3$s`", account1.getId(), account2.getId(), name));
    SiteAccount sa = new SiteAccount();
    sa.setName(name);
    sa.setBalance(account1.getBalance() + account2.getBalance());
    sa.setCreationTime(new Date());
    siteAccountDao.deleteAccount(account1);
    siteAccountDao.deleteAccount(account2);
    siteAccountDao.saveAccount(sa);
    
  }
}
